﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;
using Jungle.Code.Core;
using System.Text;
using System.Security.Cryptography;

using Jungle.Prodamo.Core;
using Jungle.Prodamo.MostNeedFunctions;

	public partial class LoginClass : BaseUserModule
	{
        SessionKeeper thisSession = new SessionKeeper();
        protected override void OnInit(EventArgs e)
        {
            btn_login.PostBackUrl = Request.RawUrl;
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string jScript = "<script language=javascript>";
            jScript += "var servPath = \"" + UrlHelper.GetSiteUrl() + "\";";
            jScript += "</script>";

            Page.RegisterStartupScript("defJscript", jScript);

            btn_exit.PostBackUrl = Request.RawUrl;
            thisSession.SessionId = Session.SessionID.ToString();
            thisSession.GetData();

            if (thisSession.UserId == -1)
            {
                LoginMenu.Visible = true;
                LoginMenu.ActiveViewIndex = 0;
            }
            else
            {
                if (thisSession.UserId != -2)
                {
                    LoginMenu.Visible = true;
                    LoginMenu.ActiveViewIndex = 1;
                    lbGreat.Text = "Здравствуйте, " + thisSession.UserLogin;
                }
            }

        }
        protected void Logout(object sender, EventArgs e)
        {
            thisSession.DeleteSession(Session.SessionID.ToString());
            Response.Redirect(UrlHelper.GetSiteUrl());
        }
        protected void Login(object sender, EventArgs e)
        {
            UserManager um = new UserManager();
            um.NickName = tb_login.Text;
            um.Password = Functions.getMd5Hash(tb_password.Text.Trim());

            if (um.Login())
            {
                thisSession.SessionId = Session.SessionID.ToString();
                thisSession.UserId = um.UserId;
                thisSession.UserLogin = um.NickName;
                thisSession.AdminLogin = "none";

                thisSession.Create();

                Response.Redirect(UrlHelper.GetSiteUrl());
            }
            else
            {
                Login_MSG.Visible = true;
                Login_MSG.InnerHtml = "Неверный логин или пароль";
            }
        }
	}
